/*
https://leetcode-cn.com/problems/largest-palindrome-product/solution/mei-ju-shu-xue-by-megurine-p1bn/
 */
public class Solution479 {
    public int largestPalindrome(int n) {
        if (n==1){
            return 9;
        }
        long p= (long) Math.pow(10,n);
        for (long x=2;x<p;x++){
            String s=String.valueOf(p-x);
            String s2=new StringBuilder(s).reverse().toString();
            long l=Long.parseLong(s2);
            long tmp=x*x-4*l;
            if (tmp<0){
                continue;
            }
            long sqr= (long) Math.sqrt(tmp);
            if (sqr*sqr!=tmp){
                continue;
            }
            return (int)(((p-x)*p+l)%1337);
        }
        return -1;
    }

    public static void main(String[] args) {
        System.out.println(new Solution479().largestPalindrome(2));
    }
}
